﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolrNet.Impl;
using SolrNet.Commands;
using SolrNet.Commands.Parameters;

namespace SolrExample.Tests.SolrClient
{
    public class SearchEngineCommand
    {
        private const char SearchEngineUrlSeparator = '#';

        /// <summary>
        /// Delete From Index_id
        /// </summary>
        /// <param name="Id"></param>        
        public Dictionary<string, bool> Delete(List<string> searchEngineUrls, string recordId, string countryCode, bool canCommit)
        {
            return Delete(searchEngineUrls, new List<string>() { recordId }, countryCode, canCommit);
        }

        /// <summary>
        /// Delete IdList
        /// </summary>
        /// <param name="idList"></param>
        public Dictionary<string, bool> Delete(List<string> searchEngineUrls, IList<string> recordIdList, string countryCode, bool canCommit)
        {
            Dictionary<string, bool> results = new Dictionary<string, bool>();

            foreach (string searchEngineUrl in searchEngineUrls)
            {
                var searchEngineUrlParts = searchEngineUrl.Split(SearchEngineUrlSeparator);
                string searchEngineCodePart = searchEngineUrlParts[0];
                string searchEngineUrlPart = searchEngineUrlParts[1];
                var connection = new SolrConnection(searchEngineUrlPart + "/" + countryCode);

                try
                {
                    var deleteCommand = new DeleteCommand(new DeleteByIdAndOrQueryParam(recordIdList, null, null));
                    deleteCommand.Execute(connection);

                    if (canCommit)
                    {
                        CommitCommand commitCommand = new CommitCommand();
                        string commandResult = commitCommand.Execute(connection);
                    }
                    results.Add(searchEngineCodePart, true);
                }
                catch (Exception ex)
                {
                    results.Add(searchEngineCodePart, false);
                }
            }

            return results;
        }

        /// <summary>
        /// Delete SearchEngine Db From Search Key
        /// </summary>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        public Dictionary<string, bool> DeleteSearchKey(List<string> searchEngineUrls, string searchKey, string countryCode, bool canCommit)
        {
            Dictionary<string, bool> dic = new Dictionary<string, bool>();
            StringBuilder sbComm = new StringBuilder();
            sbComm.AppendFormat("<delete><query>{0}</query></delete>", searchKey);
            dic = PostXml(searchEngineUrls, sbComm.ToString(), countryCode, canCommit);
            return dic;
        }

        public Dictionary<string, bool> PostXml(List<string> searchEngineUrls, string indexFieldXml, string countryCode, bool canCommit)
        {
            Dictionary<string, bool> results = new Dictionary<string, bool>();
            foreach (string searchEngineUrl in searchEngineUrls)
            {
                var searchEngineData = searchEngineUrl.Split(SearchEngineUrlSeparator);
                string engineCode = searchEngineData[0].Trim();
                string engineUrl = searchEngineData[1].Trim() + "/" + countryCode;
                var connection = 
                    new SolrConnection(engineUrl);
                try
                {
                    connection.Post("/update", indexFieldXml);
                    if (canCommit)
                    {
                        //connection.Post("/update", "<commit />");
                        CommitCommand commitCommand = new CommitCommand();
                        string commandResult = commitCommand.Execute(connection);
                    }
                    results.Add(engineCode, true);
                }
                catch (Exception ex)
                {
                    results.Add(engineCode, false);
                }
            }
            return results;
        }
    }
}
